//
//  PlayerViewController.m
//  MusicPlayer-two
//
//  Created by lanou3g on 15/8/21.
//  Copyright (c) 2015年 大展宏图. All rights reserved.
//

#define  HJLog(...) NSLog(__VA_ARGS__)
#import "PlayerViewController.h"
#import "PlayerManager.h"
#import "UIImageView+WebCache.h"
#import "NSString+NSString_Extension.h"

@interface PlayerViewController ()< UITableViewDelegate, UITableViewDataSource,PlayerManagerDelegate >



@end

@implementation PlayerViewController

static PlayerViewController *s_playerViewController = nil ;
+ (PlayerViewController *)playerViewControllerManager{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        s_playerViewController = [[PlayerViewController alloc]init];
    });
    return s_playerViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.player = [[Player alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.player.backgroundColor = [UIColor whiteColor];
    self.view = _player;
    self.player.lyricTableView.delegate = self ;
    self.player.lyricTableView.dataSource = self ;
    [PlayerManager defaultManager].delegate = self;
   
    
    // 去掉TableView下划线
    self.player.lyricTableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    
    self.player.playerScrollView.pagingEnabled = YES;
    self.edgesForExtendedLayout = 0 ;
    
    // 给歌词添加背景图片
    UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"girl10.jpg"];
    [view addSubview:imageView];
    self.player.lyricTableView.backgroundView = view;
    
    
    // 初始化声音...
     [[PlayerManager defaultManager] musciChangeWithValumn:5.0];
   
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self reloadViews];
    
    
   }



- (void)musicPlayingWithCurrentSeconds:(NSInteger)seconds{

    //改变图片角度
    [_player.singerImage setTransform:CGAffineTransformRotate(_player.singerImage.transform, M_PI / 180)];
    
    //获取当前歌词
    NSArray *lyrics = [PlayerManager defaultManager].currentMusic.timeForLyric;
    
    // 获取当前时间
    NSString *currentTime = [NSString stringWithSecondsInteger:seconds];
    
    [self.player changeCurrentTimeLabel:currentTime];
    
    [self.player changeTimeSliderVale:seconds];
    
    for (NSInteger i = 0; i < lyrics.count; i++) {
        NSDictionary *dic = lyrics[i];
        
        NSString *timeKey = [[dic allKeys] lastObject];
        
        if ([timeKey isEqualToString:currentTime]) {
            
            [_player.lyricTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        }
        
    }
    
   
    
    


}

- (void)didCutMusicComplete{
    
    [self reloadViews];
    
}


- (void)reloadViews{
    
    // 标题
    self.title = [PlayerManager defaultManager].currentMusic.name;
    // 图片
    [self.player.singerImage sd_setImageWithURL:[NSURL URLWithString:[PlayerManager defaultManager].currentMusic.picUrl]];
    
    // 模糊图片
    
    [self.player.buleImageView sd_setImageWithURL:[NSURL URLWithString:[PlayerManager defaultManager].currentMusic.blurPicUrl]];
    
    
    [self.player.lyricTableView reloadData];
    
    // 获取时间
    NSString *deuration = [PlayerManager defaultManager].currentMusic.duration;
    
    [self.player changeToatalTimeLabel:deuration];
    
    // 改变歌曲总时长
    [self.player changeTimeSliderMaxValue:[deuration integerValue] / 1000];
    
    
    
    
}




#pragma mark - TableView Delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [PlayerManager defaultManager].currentMusic.timeForLyric.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell_id"];
    }
    cell.textLabel.text = [[[PlayerManager defaultManager].currentMusic.timeForLyric[indexPath.row] allValues] lastObject];
    
    [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
    [cell.textLabel setFont: [UIFont systemFontOfSize:15]];
    [cell.textLabel setNumberOfLines:0];
    // 去掉下边的黑线....
    
    
    // 去掉cell点击的黑框
    UIView *view = [[UIView alloc]initWithFrame:cell.selectedBackgroundView.frame];
    view.backgroundColor = [UIColor clearColor];
    cell.selectedBackgroundView = view;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    
   // 当前歌词高亮
    cell.textLabel.highlightedTextColor = [UIColor cyanColor];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 30;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
